package com.ruoyi.framework.web.exception.base;

import java.util.List;

/**
 * 分页返回结果扩展类
 */
public class XPage {
    /**
     * 总页数
     */
    private int totalPage;
    /**
     * 当前页数
     */
    private int page;
    /**
     * 当前页的数量
     */
    private int total;
    /**
     * 当前页的数据集合
     */
    private List data;

    public XPage(Page page) {
        if (page != null) {
            this.totalPage = page.getTotalPage();
            this.page = page.getPageNo();
            this.total = page.getList().size();
            this.data = page.getList();
        } else {
            throw new IllegalArgumentException("数据异常");
        }
    }

    /**
     * 分页视图类创建
     *
     * @param totalPage 总页数
     * @param page      页数
     * @param data      数据
     */
    public XPage(int totalPage, int page, List data) {
        this.totalPage = totalPage;
        this.page = page;
        if (data != null) this.total = data.size();
        this.data = data;
    }

    /**
     * 分页视图类创建
     *
     * @param page 分页实体类
     * @param data 数据
     */
    public XPage(Page page, List data) {
        try {
            this.totalPage = (int) (page.getCount() / page.getPageSize()) + (page.getCount() % page.getPageSize() == 0 ? 0 : 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.page = page.getPageNo();
        if (this.totalPage >= this.page) {
            if (data != null) this.total = data.size();
            this.data = data;
        }
    }

    /**
     * 分页视图类创建
     *
     * @param map  分页实体类
     * @param data 数据
     */
    public XPage(XParamMap map, List data) {
        this.totalPage = map.getTotalPage();
        this.page = map.getCurrentPage();
        if (this.totalPage >= this.page) {
            if (data != null) this.total = data.size();
            this.data = data;
        }
    }


    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int count) {
        this.total = total;
    }

    public List getData() {
        return data;
    }

    public void setData(List data) {
        this.data = data;
    }
}
